#include <iostream>
#include "Length.h"
#include "Unit.h"

Length::Length(int length, const Unit& unit)
    :length_m(length),unit_m(unit)
{
    mile_m = calcMile();
    yard_m = calcYard();
    feet_m = calcFeet();
}

int Length::convertToBaseUnit() const
{ 
   return length_m*unit_m.factor_m;
}

int Length::calcMile()
{
   return convertToBaseUnit()/MILE.factor_m;
}

int Length::calcYard()
{
   return (convertToBaseUnit()%MILE.factor_m)/YARD.factor_m;
}

int Length::calcFeet()
{
   return (convertToBaseUnit()%YARD.factor_m)/FEET.factor_m;
}

void Length::display() const
{
   if(mile_m > 0)  std::cout<< mile_m << "Mile ";
   if(yard_m > 0)  std::cout<< yard_m << "Yard ";
   if(feet_m > 0)  std::cout<< feet_m << "Feet ";
   std::cout << std::endl;
}

bool Length::operator==(const Length& length) const
{
    return convertToBaseUnit() == length.convertToBaseUnit();
}

Length operator+(const Length &a, const Length &b)
{
    return Length(a.convertToBaseUnit()+b.convertToBaseUnit(),FEET);
}

